import * as Cesium from '@cesiumjs';

/* 303 */
/***/

var _toConsumableArray = function(arr) {
  if (Array.isArray(arr)) {
    for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
      arr2[i] = arr[i];
    }

    return arr2;
  } else {
    console.error("Array required!")
    return [];
  }
};

var hpr_camera = void 0;
var hpr_origin = void 0;
var hpr_destination = void 0;
var hpr_dir = void 0;
var hpr_up = void 0;
var hpr_result = void 0;

// 注意返回的heading为0时，朝东。
// vtxf(15017528)  9:36:48
// 注意这里的origin和destination是Cartesian3，即世界坐标系下的坐标点
// vtxf(15017528)  9:37:01
// 返回值是一个数据 [heading, pitch, roll]
function hpr(origin, destination, result) {
  try {
    var _Cesium$Cartesian, _Cesium$Cartesian2;

    hpr_camera = hpr_camera || new Cesium.Camera(window.xbsj_hpr_scene); // xbsj_hpr_scene是个很无奈的选择。。
    hpr_origin = hpr_origin || new Cesium.Cartesian3();
    hpr_destination = hpr_destination || new Cesium.Cartesian3();
    hpr_dir = hpr_dir || new Cesium.Cartesian3();
    hpr_up = hpr_up || new Cesium.Cartesian3();
    hpr_result = hpr_result || new Cesium.HeadingPitchRoll();

    var oc = (_Cesium$Cartesian = Cesium.Cartesian3).fromRadians.apply(_Cesium$Cartesian, _toConsumableArray(origin)
      .concat([undefined, hpr_origin]));
    var dc = (_Cesium$Cartesian2 = Cesium.Cartesian3).fromRadians.apply(_Cesium$Cartesian2, _toConsumableArray(
      destination).concat([undefined, hpr_destination]));

    if (Cesium.Cartesian3.equals(oc, dc)) {
      return undefined;
    }

    var dir = Cesium.Cartesian3.subtract(dc, oc, hpr_dir);
    dir = Cesium.Cartesian3.normalize(dir, dir);

    var up = Cesium.Cartesian3.normalize(oc, hpr_up);
    up = Cesium.Cartesian3.normalize(up, up);

    hpr_camera.setView({
      destination: oc,
      orientation: {
        direction: dir,
        up: up
      }
    });

    result = result || [0, 0, 0];

    var heading = hpr_camera.heading;
    heading -= Cesium.Math.PI_OVER_TWO;
    if (heading < 0) {
      heading += Cesium.Math.TWO_PI;
    }

    result.splice(0, result.length, heading, hpr_camera.pitch, hpr_camera.roll);

    return result;
  } catch (error) {
    return undefined;
  }
}

export default hpr;
